﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Rose.Models;
using Rose.Helper;

namespace Rose.Controllers
{
    [Authorize]
    public class ArticleController : Controller
    {
        //
        // GET: /Article/
        ///null ---self----0
        ///true-----friend----1
        ///false----vistor----2
        ///[]
        private Database_RoseEntities db = new Database_RoseEntities();
        private ArticleHelpModel articlehelpmodel = new ArticleHelpModel();
        public ActionResult Index()
        {
            ViewData["PageTitle"] = "All Article";
            

            string selfId=new CheckHelpModel().CheckCurrentUserId();
            const int PAGESIZE = 20;

            int allcount = articlehelpmodel.GetArticleCount(selfId,null);
            ViewData["AllCount"] = allcount;
            ViewData["MaxPage"] = allcount % PAGESIZE == 0 ? allcount / PAGESIZE : allcount / PAGESIZE + 1;

            List<ArticleAbstractModel> model = articlehelpmodel.GetArticleAbstractModelList(selfId, null, 1, PAGESIZE);
            return View(model);
        }
        
        public ActionResult AddArticle()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddArticle(ArticleModel model)
        {
            string selfId = new CheckHelpModel().CheckCurrentUserId();

            articlehelpmodel.AddArticleToDatabase(model, selfId);
            return RedirectToAction("Index", "Article");
        }


        [HttpPost]
        public ActionResult DeleteArticle(string articleId)
        {
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            if (articlehelpmodel.RemoveArticleFromDatabase(articleId, selfId))
                return Content("删除成功");
            return Content("删除失败");
        }


        public ActionResult UpdateArticle(string articleId)
        {
            ViewData["PageTitle"] = "Update Article";
            string selfId = new CheckHelpModel().CheckCurrentUserId();
            UserInfo selfInfo =new CheckHelpModel().CheckUserInfo(selfId);
            ArticleModel returnModel = articlehelpmodel.GetArticleModel(articleId, null);

            if(selfInfo.Name==returnModel.Author)
                  return View(returnModel);
            return Content("你无权修改");

            ////防止不良用户更新他人的文章
           
        }

        [HttpPost]
        public ActionResult UpdateArticle(ArticleModel article)
        {
            string selfId = new CheckHelpModel().CheckCurrentUserId();

            articlehelpmodel.UpdateArticle(article, selfId);
            
            return RedirectToAction("Index", "Article");
        }

        public ActionResult SingleArticlePart(ArticleModel model)
        {
            return View(model);
        }
        public ActionResult ArticleScriptPart()
        {
            return View();
        }
        
    }
}
